
//
//  OBDViewController.m
//  CarCare
//
//  Created by ileo on 14-9-20.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "OBDViewController.h"
//#import "NetCar.h"
#import "UserHelper.h"
#import "FLabel.h"


#define HEIGHT (__SCREEN_IS_3_5?35:40)

#define TIME_WAIT 120

@interface OBDViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *picBGV;

@property (nonatomic, strong) UIButton *button;

@end

@implementation OBDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = LOCSTRING(@"obd_title");
    
    UIImageView *imageV = [Factory imageViewWithFrame:CGRectMake(0,__SCREEN_IS_3_5?0: 20, 320*__MAIN_RATIO_320, 218*__MAIN_RATIO_320) image:ImageNamed(@"obd.png") onView:self.view];
    
    CGFloat gap = 0;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(gap, imageV.bottom + (__SCREEN_IS_3_5?10: 20), __MAIN_WIDTH - 2 *gap, HEIGHT * 3)];
    self.tableView.scrollEnabled = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorColor = [Theme lineColor];
    
    self.button = [ViewFactory normalButtonWithFrame:FactoryRectButtonDefault(__MAIN_HEIGHT_64 - BOTTOM_SIDE) title:@"插入OBD，马上使用" inView:self.view action:^{
        [UserHelper getUserInfoSuccess:^{
            USERINFO.isLogin = YES;
            [SCREEN showMainScreen];
        } fail:^{
            USERINFO.isLogin = YES;
            [SCREEN showMainScreen];
        }];
    }];
    self.button.titleLabel.font = FONT_DEFAULT_Light(14);
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSString *text = @"";
    switch (indexPath.row) {
        case 0:{
            text = LOCSTRING(@"obd_tip_1");
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 14, 14)];
            view.layer.cornerRadius = view.height/2;
            view.backgroundColor = [UIColor colorWithHexString:@"#41aaec"];
            [Factory labelWithFrame:CGRectMake(0, 0, view.width, view.height) font:[UIFont systemFontOfSize:12] text:@"1" textColor:[UIColor whiteColor] onView:view textAlignment:NSTextAlignmentCenter];
            [cell.contentView addSubview:view];
            view.center = CGPointMake(tableView.width - 80, HEIGHT/2);
        }
            break;

        case 1:
        {
            text = LOCSTRING(@"obd_tip_2");
            UIView *goView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 12)];
            goView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"go.png"]];
            [cell.contentView addSubview:goView];
            goView.center = CGPointMake(tableView.width - 20, HEIGHT/2);
        }
            break;
            
        case 2:
            text = LOCSTRING(@"obd_tip_3");
            break;
            
        default:
            break;
    }
    
    [Factory labelWithFrame:CGRectMake(20, 0, tableView.width - 20, HEIGHT) font:FONT_DEFAULT_Light(14) text:text textColor:[UIColor lightGrayColor] onView:cell textAlignment:NSTextAlignmentLeft];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 1:
            [self.view addSubview:self.picBGV];
            break;
            
        default:
            break;
    }
}

-(UIView *)picBGV{
    if (!_picBGV) {
        _picBGV = [[UIView alloc] initWithFrame:self.view.bounds];
        _picBGV.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
        [Factory imageViewWithCenter:_picBGV.center image:ImageNamed(@"true_obd.png") onView:_picBGV];
        
       	UITapGestureRecognizer *tapRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickOnBG)];
        [_picBGV addGestureRecognizer:tapRecognizer1];
    }
    return _picBGV;
}

-(void)clickOnBG{
    [self.picBGV removeFromSuperview];
}

@end
